const axios = require('../../utils/request')

Page({
    data: {
        userInfo:{

        },
        auth_code:''
    },
    onLoad(){
        this.grantPublishPopup = this.selectComponent('#grantPublishPopup')
        
    },
    onShow(){
        let userInfo = wx.getStorageSync('userInfo')
        this.setData({
            userInfo:userInfo
        })
        axios.post('/api/user/userinfo').then(res=>{
            if(res.code==1){
                wx.setStorageSync('userInfo',res.data.userInfo)
                this.setData({userInfo:res.data.userInfo})
            }
        })
    },
    onShareAppMessage(e){
        if(e.from=="button"){
            return {
                title:'发布新人奖',
                path:'/pages/publishDetail/index?auth_code='+this.data.auth_code
            }
        }
    },
    onGrantPublish(){
        wx.showLoading({
            title:'生成授权码...'
        })
        axios.post('/api/authorize/create').then(res=>{
            wx.hideLoading()
            if(res.code==1){
                this.setData({
                    auth_code:res.data.code
                })
                this.grantPublishPopup.onHandlePopup()
            }else{
                wx.showToast({
                    title:res.msg,
                    icon:'none'
                })
            }
        })
        // this.grantPublishPopup.onHandlePopup()
    },
    closeGrantPublishPopup(){
        this.grantPublishPopup.onClosePopup()
    },
    toDetail(){
        wx.navigateTo({
            url:'/pages/detail/index'
        })
    },
    toMyReward(){
        wx.navigateTo({
            url:'/pages/myReward/index'
        })
    },
    toAddress(){
        wx.chooseAddress({
            success:res=>{
                if(res.errMsg=="chooseAddress:ok"){
                    wx.showLoading({
                        mask:true,
                        title:'设置中...'
                    })
                    delete res.errMsg
                    axios.post('/api/adress/add',res).then(res=>{
                        if(res.code==1){
                            wx.hideLoading()
                            wx.showToast({
                                title:'设置成功'
                            })
                        }
                    })
                }
            }
        })
    },
    toBalance(){
        wx.navigateTo({
            url:'/pages/balance/index'
        })
    },
    toSetProvider(){
        wx.navigateTo({
            url:'/pages/setProvider/index'
        })
    },
    toContact(){
        wx.navigateTo({
            url:'/pages/contact/index'
        })
    },
    toMyPublished(){
        wx.navigateTo({
            url:'/pages/myPublished/index'
        })
    },
    toMyParticipated(){
        wx.navigateTo({
            url:'/pages/myParticipated/index'
        })
    }
})
